import axios from 'axios'
import {
  get,
  post
} from '@/utils/request'

const prefix = '/shop'

/**
 * 获取商品详情
 * @param {*} id 商品ID
 */
export const getSpu = id => get(`${prefix}/spu/${id}`)

/**
 * 获取购物车列表
 */
export const cart = function () {
  return get(`${prefix}/cart`)
}

/**
 * 批量更新购物车选中状态
 */
export const batchUpdateCartStatus = function (data) {
  return post(`${prefix}/cart/checks`, data, true)
}

/**
 * 清除购物车无效商品
 * @param Array data 购物车条目ID数组
 * ```
 * {
 *   ids: Array
 * }
 * ```
 */
export const clearCart = (data) => post('/shop/cart/delete', {
  ids: data
})

/**
 * 添加商品进购物车
 * @param {*} data 商品信息
 */
export const addCart = function (data) {
  return post(`${prefix}/cart/add`, data)
}

/**
 * 加入购物车
 * @param data 参数
 * ```
 * {
 *   product_id: 商品ID,
 *   goods_id: SKU ID,
 *   nums: 数量
 * }
 * ```
 */
export const postAddCartApi = (data) => axios({
  method: 'post',
  url: '/shop/cart/add',
  data: data,
  headers: {
    FastMode: true
  }
})

/**
 * 开始结算购物车
 * @param data 参数
 * ```
 * {
 *   product_id: 商品ID,
 *   goods_id: SKU ID,
 *   nums: 数量
 * }
 * ```
 */
export const cartBalance = (data) => post('/shop/cart/balance', {
  items: data
})

/**
 * 获取预结算订单信息
 */
export const getPackBalance = () => get('/shop/cart/pack')

/**
 * 获取预结算订单信息
 * @param data 参数
 * ```
 * {
 *   areacode: 商品ID,
 *   items: SKU ID
 * }
 * ```
 */
export const reckonOrderExpress = (data) => post('/shop/express/reckon', data, true)

/**
 * 创建购物订单
 * @param data 参数
 * ```
 * {
 *   areacode: 商品ID,
 *   items: SKU ID
 * }
 * ```
 */
export const createOrder = (data) => post('/shop/order/create', data, true)

/**
 * 获取商品详情
 * @param id 商品ID
 */
export const orderList = (data) => {
  return get('/shop/order', data)
}

/**
 * export const getProductInfo = (id) => axios({
  method: 'get',
  url: '/shop/spu/' + id,
  params: {},
  headers: {}
})
*/

/**
 * 获取商品详情
 * @param id 商品ID
 */
export const getProductInfoApi = (id) => {
  return axios.get('/shop/spu/' + id)
}

/**
 * 获取当前购物车商品数量
 * @param none
 */
export const getCartNumberApi = () => axios({
  method: 'get',
  url: '/shop/cart/number',
  headers: {
    FastMode: true
  }
})

/**
 * 商城收藏商品、店铺
 * @param data 参数信息
 * ```
 * {
 *   product_id: 商品ID,
 *   store_id: 店铺ID
 * }
 * ```
 */
export const collect = (data) => post('/shop/extend/collect', data, true)

/**
 * 客服
 */
export const getService = () => get('/app/service/connect')
